#!/bin/bash
#@ 自动安装nginx 脚本

NGINX_BASE=/var/www
NGINX_VERSION=nginx-1.16.0

yum install -y openssl openssl-devel pcre-devel gd-devel gcc-c++

####查询nginx用户是否存在####
user=nginx 
group=nginx

#create group if not exists  
egrep "^$group" /etc/group >& /dev/null  
if [ $? -ne 0 ]  
then  
    groupadd -r -g 80 $group  
fi  

#create user if not exists  
egrep "^$user" /etc/passwd >& /dev/null  
if [ $? -ne 0 ]  
then  
    useradd -r -u 80 -g 80 -M -s /sbin/nologin $user  
fi  


if [ -d $NGINX_BASE ]
then
    echo "Dir already exists"
else
    echo "Dir don't exists"
    mkdir $NGINX_BASE
fi
tar xf ${NGINX_VERSION}.tar.gz
cd ${NGINX_VERSION}

./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-stream --with-stream_ssl_module --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre

make && make install

cp -r /usr/local/nginx/html ${NGINX_BASE}

touch /lib/systemd/system/nginx.service
cat > /lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl enable nginx

systemctl start nginx





